package sword.T18;

import util.ListNode;

/**
 * @File Info: leetcode -- <Solution>
 * @Author: 18362
 * @Create: 2022-03-23 21:12:25 星期三
 */

class Solution {
    public ListNode deleteNode(ListNode head, int val) {
        ListNode dummy = new ListNode(0, head);
        ListNode p = head, pre = dummy;
        while (p != null && p.val != val) {
            p = p.next;
            pre = pre.next;
        }
        if (p == null)
            return head;
        if (p.next != null) {
            p.val = p.next.val;
            p.next = p.next.next;
        } else {
            pre.next = null;
        }
        return dummy.next;
    }
}